package com.fyl.verify.spring.boot.autoconfigure;

import com.fyl.core.advance.constant.enums.CommonResponseEnum;
import com.fyl.verify.aop.VerifyAnnotationAdvisor;
import com.fyl.verify.aop.VerifyAnnotationMethodInterceptor;
import com.fyl.verify.service.VerifyService;
import com.fyl.verify.utils.SpringContextUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.*;
import org.springframework.http.HttpStatus;

@Configuration
@Import(SpringContextUtils.class)
@EnableConfigurationProperties(VerifyProperties.class)
@ConditionalOnProperty(name = "fyl.verify.enabled", havingValue = "true", matchIfMissing = true)
public class VerifyAutoConfiguration {

//    @Autowired
//    VerifyProperties verifyProperties;

    private static final Logger logger = LoggerFactory.getLogger(VerifyAutoConfiguration.class);

    @Bean
    @ConditionalOnMissingBean
    public VerifyService verifyService() {
        logger.warn("{}.authVerifyHandler 还没有实现，现在用的是默认都能通过的实现方法", VerifyService.class.getName());
        return (request, response) -> CommonResponseEnum.SUCCESS;
    }

    @Bean
    @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
    @ConditionalOnMissingBean
    public VerifyAnnotationMethodInterceptor verifyAnnotationMethodInterceptor() {
        return new VerifyAnnotationMethodInterceptor();
    }

    @Bean
    @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
    @ConditionalOnMissingBean
    public VerifyAnnotationAdvisor verifyAnnotationAdvisor(VerifyAnnotationMethodInterceptor advisor) {
        VerifyAnnotationAdvisor verifyAnnotationAdvisor = new VerifyAnnotationAdvisor(advisor);
        verifyAnnotationAdvisor.setOrder(1);
        return verifyAnnotationAdvisor;
    }

}
